package com.duke.shaking.profile;


import java.util.HashMap;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.duke.shaking.R;
import com.duke.shaking.TitleActivity;
import com.duke.shaking.net.AsyncHttpResponseWithResultHandler;
import com.duke.shaking.net.NetUtils;

public class CareerEditActivity extends TitleActivity 
		implements RadioGroup.OnCheckedChangeListener {

	private RadioGroup rg_career;
	private int selectedCareerId;
	private String selectedCareerName;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        selectedCareerId = 0;
        selectedCareerName = null;
        
        rg_career = (RadioGroup)findViewById(R.id.rg_career);
        if (rg_career != null) {
        	rg_career.setOnCheckedChangeListener(this);
        }
        
        Intent i = getIntent();
        selectedCareerId = i.getIntExtra("career", 0);
        
        initRadio(selectedCareerId);
	}
	
	private void initRadio(int career) {
		for (int i = 0; i < rg_career.getChildCount(); i++) {
			View child = rg_career.getChildAt(i);
			if (child instanceof RadioButton) {
				try {
					String tag = (String)child.getTag();
					if (Integer.parseInt(tag) == career) {
						((RadioButton)child).setChecked(true);
					}
				}
				catch(Exception ex) {
				}
			}
		}
    }
	
	@Override
	protected int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.activity_career_edit;
	}

	@Override
	protected void onTitleInit() {
		// TODO Auto-generated method stub
		initTitle(TITLE_BTN_EXIST | TITLE_BTN_BACK,
				R.string.back,
				TITLE_BTN_EXIST,
				R.string.save,
				R.string.career);
	}

	public void onCheckedChanged(RadioGroup group, int checkedId) {
		// TODO Auto-generated method stub
		
		RadioButton checked = (RadioButton)findViewById(checkedId);
		if (checked != null) {
			String tag = (String)checked.getTag();
			if (tag != null) {
				try {
					selectedCareerId = Integer.parseInt(tag);
					selectedCareerName = checked.getText().toString();
				}
				catch(Exception e) {
				}
			}
		}
	}

	public void onLeftBtn(View v) {
		Intent intent = new Intent();  
        setResult(RESULT_CANCELED, intent); 
        finish(); 
	}
	
	public void onRightBtn(View v) {
		AsyncHttpResponseWithResultHandler responseHandler = new AsyncHttpResponseWithResultHandler(){
			
        	@Override
			public void onResultSuccess(String response) {
				// TODO Auto-generated method stub
				super.onResultSuccess(response);
				
				afterSaveSuccess();
			}

			@Override
			public void onResultFailure(String result) {
				// TODO Auto-generated method stub
				super.onResultFailure(result);
				
				toast("保存失败");
			}
        };
        
        Map<String, String> params = new HashMap<String, String>();
        params.put(NetUtils.JOB, String.valueOf(selectedCareerId));
        params.put(NetUtils.TOKEN, me.getAuthToken());
        NetUtils.updateProfile(this, responseHandler, params, null);
	}
	
	private void afterSaveSuccess() {
		Intent intent = new Intent(); 
        intent.putExtra("career", selectedCareerId);
        me.setJob(selectedCareerId);
        intent.putExtra("careerName", selectedCareerName);
        setResult(RESULT_OK, intent); 
        finish(); 
	}
}
